<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: nzp
  Date: 2020/6/12
  Time: 15:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script src="${pageContext.request.contextPath}/js/jquery-3.3.1.js"></script>
<script type="text/javascript">
    function checkForm(){
        var stuCode=document.getElementById("stuCode").value;
        var name=document.getElementById("name").value;
        var sex=document.getElementById("sex").value;
        var phone=document.getElementById("phone").value;
        var grade=document.getElementById("grade").value;
        var dormBuildId=document.getElementById("dormBuildId").value;
        var did=document.getElementById("did").value;

        if(stuCode=="" ||name==""||dormBuildId==""||phone==""||grade==""||did==""||sex==""){
            document.getElementById("error").innerHTML="信息填写不完整！";
            return false;
        } else if(!/^1[2345678]\d{9}$/.test(phone)){
            document.getElementById("error").innerHTML="手机号码格式错误！";
            return false;
        }

        var didSelect = $("#did").val();
        document.getElementById("did").value = didSelect;
        return true;
    }


    function findDormByBuildName(){
        var vs = $("#dormBuildId").find("option:selected").text();
        $.post("/dormBuild/findDormByBuildName?BuildName="+vs,{},function (data) {
            var Dlis = "";
            console.log(data);
            for(var i=0;i<data.length;i++){
                Dlis += '<option value="'+data[i].id+'">'+data[i].number+'</option>';
            }
            $("#did").html(Dlis);
        })
    }
</script>

<script>
    $(function () {
        $.post("/dormBuild/findBuild",{},function (data) {
            var Blis = "";
            for(var i=0;i<data.length;i++){
                Blis += ' <option value="'+data[i].id+'">'+data[i].name+'</option>';
            }
            $("#dormBuildId").html(Blis);
        })

        $.post("/dormBuild/findDormByBuildName?BuildName=A",{},function (data) {
            var Dlis = "";
            console.log(data);
            for(var i=0;i<data.length;i++){
                Dlis += '<option value="'+data[i].id+'">'+data[i].number+'</option>';
            }
            $("#did").html(Dlis);
        })
    });
</script>



<div class="data_list">
    <div class="data_list_title">
        添加学生
    </div>
    <form action="${pageContext.request.contextPath}/student/addStudent" method="post" onsubmit="return checkForm()">
        <div class="data_form" >
            <div align="center">
                <font id="error" color="red"></font>
            </div>
            <table align="center">
                <tr>
                    <td><font color="red">*</font>学号：</td>
                    <td><input type="text" id="stuCode"  name="stuCode" value=""  style="margin-top:5px;height:30px;" /></td>
                </tr>
                <tr>
                    <td><font color="red">*</font>姓名：</td>
                    <td><input type="text" id="name"  name="name" value=""  style="margin-top:5px;height:30px;" /></td>
                </tr>
                <tr>
                    <td><font color="red">*</font>性别：</td>
                    <td>
                        <select id="sex" name="sex" style="width: 90px;">
                            <option value="男">男</option>
                            <option value="女">女</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td><font color="red">*</font>联系电话：</td>
                    <td><input type="text" id="phone"  name="phone" value=""  style="margin-top:5px;height:30px;" /></td>
                </tr>
                <tr>
                    <td><font color="red">*</font>班级：</td>
                    <td><input type="text" id="grade"  name="grade" value=""  style="margin-top:5px;height:30px;" /></td>
                </tr>
                <tr>
                    <td><font color="red">*</font>宿舍楼：</td>
                    <td>
                        <select id="dormBuildId" name="dormBuildId" onchange="findDormByBuildName()" style="width: 90px;" >
                        </select>
                    </td>
                </tr>

                <tr>
                    <td><font color="red">*</font>寝室号：</td>
                    <td>
                        <select id="did" name="did" style="width: 90px;">
                        </select>
                    </td>
                </tr>
            </table>
            <div align="center">
                <input type="submit" class="btn btn-primary" value="确认"/>
                &nbsp;<button class="btn btn-primary" type="button" onclick="javascript:history.back()">返回</button>
            </div>
        </div>
    </form>
</div>
